package org.icress.fusion.b2b

import java.util.Date

class SalesOrderDetail{

    def salesOrderService
    
    SalesOrder salesOrder//订单
    String serialNumber//序号
    Product product//产品，品号
    Date deliveryLimitation//交货期限
    Integer quantity//订单数量
    BigDecimal price//标准单价
    BigDecimal discount//标准折扣率
    BigDecimal specialDiscount//特价折扣
    BigDecimal finalDiscount//最终折扣
    BigDecimal finalPrice//最终单价
    BigDecimal amount//标准合计金额
    BigDecimal specialAmount//特价金额
    BigDecimal finalAmount//最终销售金额
    Boolean openDetail//主附件订价
    ContractDetail contractDetail//OA特价申请单明细
        
    static belongsTo = [salesOrder:SalesOrder]
    static hasMany = [salesOrderDetailDetails:SalesOrderDetailDetail]

    static constraints = {
        salesOrder(nullable:false)
        serialNumber(nullable:true, blank:false,unique:'salesOrder', maxSize:2)
        product(nullable:false, blank:false)
        deliveryLimitation(nullable:false, blank:false, validator:{val, obj ->
            return obj.salesOrderService.validateDeliveryLimitation(obj.salesOrder.orderDate, val, obj.product.id)
        })
        quantity(nullable:false, min : 1, validator: { val, obj -> 
            //验证单身行金额必须大于零
            def realAmount = 0

            if(obj.openDetail == true){
                obj.salesOrderDetailDetails.each{
                    realAmount += it.quantity * it.price * it.discount
                }
            }else{
                realAmount = obj.quantity * obj.price * obj.discount 
            }
            realAmount = realAmount?:0
            return (realAmount > 0)?true:false
        })
        price(nullable:false, scale:6, validator:{val, obj ->
            if(obj.openDetail == true)//开窗时不验证价格
                return true
            else//不开窗时验证价格
                return obj.salesOrderService.validatePrice(obj.product.id, val)
        })
        discount(nullable:false, scale:4)
        specialDiscount(nullable:false, scale:4, validator:{val, obj ->
            if(obj.openDetail == true)//开窗时不验证价格
                return true
            else//不开窗时验证价格
                return obj.salesOrderService.validateSpecialDiscount(obj.product.id, val)
        })
        finalDiscount(nullable:false, scale:4)
        finalPrice(nullable:false, scale:6)
        amount(nullable:false, scale:2, min :new BigDecimal(0.01))
        specialAmount(nullable:false, scale:2, min : new BigDecimal(0))
        finalAmount(nullable:false, scale:2, min : new BigDecimal(0))
        openDetail(nullable:true, validator: { val, obj -> 
            //验证子单身行数是否大于零
            if(obj.openDetail == true){
                return (obj.salesOrderDetailDetails?.size() ?: 0 > 0) ? true:false
            }
        })
        contractDetail(nullable:true)
    }
    
    String toString() {
        return "$salesOrder.serialNumber-$serialNumber"
    }

    static mapping = {
        table 'B2B_SALESORDERDETAIL'
        /*
        amount formula: 'quantity * price * discount'
        specialAmount formula : 'quantity * price * discount * special_discount'
        finalPrice formula : 'price * discount * (1 - special_discount)'
        finalDiscount formula : 'discount * (1 - special_discount)'
        finalAmount formula : 'quantity * price * discount * (1 - special_discount)'
        */
        salesOrderDetailDetails cascade:"all-delete-orphan", sort:"serialNumber", batchSize: 100
        //salesOrderDetailDetails sort:'serialNumber', order:'asc'
    }
}
